package global.com.loansapplication.ui.actviity.user;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.listener.OnDateSetListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import global.com.loansapplication.R;
import global.com.loansapplication.base.BaseActivity;
import global.com.loansapplication.bean.ClueInfo;
import global.com.loansapplication.comm.MyApplication;
import global.com.loansapplication.event.NetErrorEvent;
import global.com.loansapplication.ui.actviity.login.SelcetInfoAct;
import global.com.loansapplication.ui.actviity.user.biz.ExportBiz;
import global.com.loansapplication.ui.actviity.user.component.DaggerExportComponent;
import global.com.loansapplication.ui.actviity.user.contract.ExportContract;
import global.com.loansapplication.ui.actviity.user.module.ExportModule;
import global.com.loansapplication.ui.actviity.user.presenter.ExportPresenter;
import global.com.loansapplication.ui.widget.wheel.ChangeAddressDialog;
import global.com.loansapplication.util.AlertUtils;
import global.com.loansapplication.util.ExcelUtil;
import global.com.loansapplication.util.SysUtils;
import rx.functions.Action1;

/**
 * Created by chenjian on 18/1/8.
 */

public class ExportDataAct extends BaseActivity implements ExportContract.View {


    @Inject
    ExportPresenter presenter;
    @Inject
    ExportBiz biz;
    @Bind(R.id.export_start_time)
    TextView startTime;
    @Bind(R.id.export_end_time)
    TextView endTime;
    @Bind(R.id.export_custome_type)
    TextView customeType;
    @Bind(R.id.export_submit)
    TextView submit;
    private String[] Str = {"不限", "上班族", "企业主", "个体户"};
    private List<String> list = new ArrayList<String>();
    private ChangeAddressDialog mChangecustomDialog;
    String StartTime="";
    String EndTime="";
    String CustomeType="0";
    String CustomeType1="";

    /**
     * 是否是自定义线索
     */
    boolean mIsCustom = false;

    @Override
    protected void setRoot() {
        setTitleBar("#FFFFFFFF");
        setContentView(R.layout.activity_export_data);

    }

    @Override
    protected void setup() {
        DaggerExportComponent.builder().exportModule(new ExportModule(this)).build().inject(this);
        setStateText();
        setToolBar("导出客户", "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        mIsCustom = getIntent().getBooleanExtra("isCustom", false);

        list = Arrays.asList(Str);
        RxView.touches(startTime)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<MotionEvent>() {

                    @Override
                    public void call(MotionEvent motionEvent) {
                        TimePickerDialog timePickerDialog = AlertUtils.timePicker(ExportDataAct.this, 365, 36500, new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                startTime.setText(SysUtils.sf.format(new Date(millseconds)));
                                StartTime=SysUtils.sf.format(new Date(millseconds))+":00";
                            }
                        });
                        timePickerDialog.show(getSupportFragmentManager(), "all");
                    }
                });
        RxView.touches(endTime)
                .compose(this.<MotionEvent>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<MotionEvent>() {

                    @Override
                    public void call(MotionEvent motionEvent) {
                        TimePickerDialog timePickerDialog = AlertUtils.timePicker(ExportDataAct.this, 1, 1, new OnDateSetListener() {
                            @Override
                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
                                endTime.setText(SysUtils.sf.format(new Date(millseconds)));
                                EndTime=SysUtils.sf.format(new Date(millseconds))+":00";
                            }
                        });
                        timePickerDialog.show(getSupportFragmentManager(), "all");
                    }
                });
        RxView.clicks(customeType)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        setCustomInfo();
                    }
                });

        RxView.clicks(submit)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(new Action1<Void>() {
                    @Override
                    public void call(Void aVoid) {
                        if (TextUtils.isEmpty(StartTime)) {
                            showMsg("请选择开始时间");
                            return;
                        }
                        if (TextUtils.isEmpty(EndTime)) {
                            showMsg("请选择结束时间");
                            return;
                        }
                        if (mIsCustom) {
                            presenter.addCustom(StartTime, EndTime, CustomeType1);
                        } else {
                            presenter.addSuggest(StartTime, EndTime, CustomeType);
                        }
                    }
                });
    }

    private void setCustomInfo() {
        mChangecustomDialog = new ChangeAddressDialog(
                ExportDataAct.this, list, "请选择客户类型");
        mChangecustomDialog
                .setAddresskListener(new ChangeAddressDialog.OnAddressCListener() {
                    @Override
                    public void confirm(int position, String province) {
                        customeType.setText(list.get(position));
                        CustomeType = position + "";
                        CustomeType1 = position == 0 ? "" : list.get(position);
                        mChangecustomDialog.dismiss();
                        mChangecustomDialog = null;
                    }

                    @Override
                    public void cancel() {
                        mChangecustomDialog.dismiss();
                        mChangecustomDialog = null;
                    }
                });
        mChangecustomDialog.show();
    }



    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    /***
     * Stop location service
     */
    @Override
    protected void onStop() {

        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();

    }

    @Override
    public void saveData(List<ClueInfo> list) {
        try {
            ExcelUtil.writeExcel(ExportDataAct.this, list, System.currentTimeMillis()+"");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
